#include <REGX52.h>
#include "AT24C02.h"
#include "SoleKey.h"
#include "LCD1602.h"
#include "Delay.h"


void main()
{
	unsigned char Key = 0;
	unsigned int Count = 0;
	LCD_Init();//LCD1602初始化
	LCD_ShowNum(1, 1, Count, 5);
    while(1)
    {
        Key = SoleKey();//获取哪个按键按下
		if(Key)
		{
			if(Key == 1)//1按键按下，Count++
			{
				Count++;
				LCD_ShowNum(1, 1, Count, 5);
			}
			else if(Key == 2)//2按键按下，Count--
			{
				Count--;
				LCD_ShowNum(1, 1, Count, 5);
			}
			else if(Key == 3)//3按键按下，存储数据
			{
				AT24C02_WriteByte(0x00, Count / 256);
				Delayms(5);//因为写速度较慢，不能连续写，会导致数据错误
				AT24C02_WriteByte(0x01, Count % 256);
				LCD_ShowNum(1, 1, Count, 5);	
				//显示写入成功
				LCD_ShowString(2, 1, "Write OK!");
				Delayms(1000);
				LCD_ShowString(2, 1, "         ");
			}
			else if(Key == 4)//4按键按下，读出数据
			{
				Count = AT24C02_ReadByte(0x00) * 256 + AT24C02_ReadByte(0x01);
				LCD_ShowNum(1, 1, Count, 5);
				//显示读出成功
				LCD_ShowString(2, 1, "Read OK! ");
				Delayms(1000);
				LCD_ShowString(2, 1, "         ");
			}
		}
		
    }
}